<template>
  <div>
    <div class="btn" style="margin-left: 30px;">
      <el-button @click="toAdd" type="primary">新增</el-button>
    </div>

    <div class="table" style="margin-top: 20px;">
      <el-table v-loading="loading" element-loading-text="拼命加载中" element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(0, 0, 0, 0.8)" :data="swipeData" style="width: 100%">
        <el-table-column type="index" label="序号" width="100" align="center">
        </el-table-column>

        <el-table-column label="站点轮播图" width="300" align="center">
          <template v-slot="scope">
            <div>
              <img style="border-radius: 5px;" height="60px" width="180px" :src="scope.row.url" alt="">
            </div>
          </template>
        </el-table-column>

        <el-table-column label="轮播图信息" width="750px" align="center">
          <template v-slot="scope">
            <div>
              <div>{{ scope.row.name }}</div>
              <div>{{ scope.row.introduce }}</div>
            </div>
          </template>
        </el-table-column>

        <el-table-column label="操作" align="center">
          <template v-slot="scope">
            <div>
              <el-link @click="toEdit(scope.row)" style="font-size: 13px; margin-right: 5px;" type="success"
                :underline="false">编辑</el-link>
              <el-link @click="toDelete(scope.row.id)" style="font-size: 13px;" type="danger"
                :underline="false">删除</el-link>
            </div>
          </template>

        </el-table-column>
      </el-table>
    </div>

    <el-dialog @close="resetField" width="80%" :title="title" :visible.sync="visible">
      <el-form :model="form" :rules="rules" ref="myForm">
        <!-- {{ form }} -->
        <el-row>
          <el-col :span="12">
            <el-form-item prop="name" label="轮播图名称" :label-width="formLabelWidth">
              <el-input v-model="form.name" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item prop="introduce" label="轮播图介绍" :label-width="formLabelWidth" style="margin-top: 40px;">
              <el-input type="textarea" v-model="form.introduce"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item prop="url" label='轮播图上传' :label-width='formLabelWidth'>
              <el-upload class="avatar-uploader" action="http://121.4.83.32:7006/UploadControlle/upload"
                :show-file-list="false" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
                <img v-if="imageUrl" :src="imageUrl" class="avatar">
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取 消</el-button>
        <el-button @click="toSave" type="primary">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { get, post } from '@/utils/request'
export default {
  data() {
    return {
      swipeData: [],
      title: '新增轮播图信息',
      visible: false,
      // 表单对象 --->将来新增或者修改提交给后端的表单对象
      form: {},
      formLabelWidth: '120px',
      // 展示图片变量
      imageUrl: '',
      // 表单校验规则
        rules: {
          name: [
            { required: true, message: '请输入轮播图名称', trigger: 'blur' }
          ], introduce: [
            { required: true, message: '请输入轮播图介绍', trigger: 'blur' }
          ], url: [
            { required: true, message: '请输入轮播图图片', trigger: 'change' }
          ]
        },
      // 加载动画变量
      loading: true
    }
  },
  methods: {
    // 编辑轮播图
    toEdit(row) {
      this.visible = true;
      this.title = '编辑轮播图信息'
      this.form = { ...row };
      this.imageUrl = this.form.url
    },
    // 删除轮播图
    toDelete(id) {
      // console.log(id);
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        let res = await get('/carousel/deleteById', { id })
        if (res.status === 200) {
          // console.log(res);
          this.getSwipe();
          this.$message({
            type: 'success',
            message: res.message
          });
        } else {
          this.$message.error(res.message)
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    resetField() {
      this.$refs.myForm.resetFields();
    },
    // 获取所有轮播图方法
    async getSwipe() {
      this.loading = true;
      let res = await get('/carousel/findAll');
      // console.log(res);
      this.swipeData = res.data;
      // console.log(this.swipeData);
      this.loading = false
    },
    // 新增 弹出模态框
    toAdd() {
      this.visible = true;
      this.form = {}
      this.imageUrl = ''
      this.title = '新增轮播图信息'
    },
    // 图片上传处理函数
    handleAvatarSuccess(res, file) {
      console.log(res, file);
      this.imageUrl = URL.createObjectURL(file.raw);
      this.form.url = res.data
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    },
    // 去保存
    toSave() {
      this.$refs.myForm.validate(async (valid) => {
        if (valid) {
          let res = await post('/carousel/saveOrUpdate', this.form)
          console.log(this.form);
          
          if (res.status === 200) {
            this.getSwipe();
            this.$message({
              type: 'success',
              message: res.message
            })
          } else {
            this.$message.error(res.message)
          }
          this.visible = false
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },

  },
  created() {
    this.getSwipe();
  }
}
</script>